#!/bin/bash

## hyphop ##

#= make install modules


D=$(realpath "$(dirname "$0")") # "
. $D/config.conf

export INSTALL_MOD_PATH=$KERNEL_OUTPUT_DIR
export INSTALL_MOD_STRIP=1 

echo "[i] install modules to $KERNEL_OUTPUT_DIR"

CMD(){
    echo "[#] $@">&2
    "$@"
}

MD5(){
    truncate -s0 "$1".md5sum
    md5sum "$1" > "$1".md5sum
}

#[ "" ]&&{

#CMD 
rm -rd $INSTALL_MOD_PATH/lib/modules 2>/dev/null

CMD ${0%_modules} modules_install 1>/dev/null

cd $INSTALL_MOD_PATH

P=$PWD

#tar -cJf modules.tar.xz lib

[ $TAR ] && {
CMD tar -czf modules.tar.gz lib
MD5 modules.tar.gz
}

cd $PWD

cd $INSTALL_MOD_PATH/lib/modules/
#cd $INSTALL_MOD_PATH/lib/modules/*

for v in ?.*.*; do
    [ "$v" = "0.0.0" ] || {
	CMD ln -sf "$v" 0.0.0
    }
done

cd 0.0.0

rm build source

CMD find -name \*.ko > modules.list

echo "!!BEGIN"

while read m ; do
    k=$(basename $m)
    n=${m#./}
    l=$(perl -e "\$_='$n'; s/\\w+\//..\//g; print \$_")
#    echo "[i] $k - $n - $l"
    mv $n .
    ln -sf $l $m

done < modules.list

echo "!!DONE"

cd $PWD

cd $INSTALL_MOD_PATH

#}

#tar -cJf modules.openwrt.tar.xz lib

[ "$TAR" ] && {
CMD tar -czf modules.openwrt.tar.gz lib
MD5 modules.openwrt.tar.gz
}

#[ "$IPK" ] && {
#$D/make_kernel_opkgs
#}

cd $PWD
ls -l1 *.gz
